<?php if(!defined('PUBLIC_DIR')) die('No Access!');
using('System.Data');

Cnamespace('System');
abstract class Markup{
    public static function generateErrorMarkup($message, $type = '', $file = '', $line = '', $function = array()) {
		$function[] = __FUNCTION__;
        $body = '<p>The application could not run because of the following error:</p>';
        $body .= '<h2>Details:</h2>';
		$body .= '<strong>Message:</strong>';
		$body .= $message;
		$body .= '<br/>';
        if ($file !== '') $body .= '<strong>File:</strong> ';
        if ($file !== '') $body .= $file;
        if ($file !== '') $body .= "<br/>\n";
        if ($line !== '') $body .= '<strong>Line:</strong> ';
        if ($line !== '') $body .= $line;
        if ($line !== '') $body .= "<br/>\n";
		$body .= '<h2>Server:</h2>';
		$body .= '<strong>Time:</strong> ';
		$body .= round(microtime(true) - TS, 4);
		$body .= " s<br/>\n";
		$body .= '<strong>Memory:</strong> ';
		$body .= Units::Data(memory_get_usage(), 'B');
		$body .= "<br/>\n";
        $body .= "<strong>PHP:</strong> ";
		$body .= PHP_VERSION;
		$body .= "<br/>\n";
		$body .= "<strong>System:</strong> ";
		$body .= PHP_OS;
		$body .= "<br/>\n";
		$trace = self::generateTraceMarkup($function);
        if ($trace !== '') $body .= '<h2>Stack Trace:</h2>' . nl2br($trace);
		//$con = get_defined_constants(true);
		//echo nl2br(var_dump($con['user']));
        return self::generateErrorTemplateMarkup('Clomibep Application Error', $body);
    }

    public static function generateDebugExceptionMarkup($exception, $function = array()) {
		$function[] = __FUNCTION__;
        $body = '<p>The application could not run because of the following exception:</p>';
        $body .= '<h2>Details:</h2>';
		$body .= '<strong>Message:</strong> ';
		$body .= $exception->getMessage();
		$body .= '<br/>';
		$file = $exception->getFile();
        if ($file !== '') $body .= '<strong>File:</strong> ';
        if ($file !== '') $body .= $file;
        if ($file !== '') $body .= "<br/>\n";
		$line = $exception->getLine();
        if ($line !== '') $body .= '<strong>Line:</strong> ';
        if ($line !== '') $body .= $line;
        if ($line !== '') $body .= "<br/>\n";
		$body .= '<br/>';
		$body .= "<h2>Server:</h2>";
		$body .= "<strong>Time:</strong> ";
		$body .= round(microtime(true) - TS, 4);
		$body .= " s<br/>\n";
		$body .= "<strong>Memory:</strong> ";
		$body .= Units::Data(memory_get_usage(), 'B');
		$body .= "<br/>\n";
        $body .= "<strong>PHP:</strong> ";
		$body .= PHP_VERSION;
		$body .= "<br/>\n";
		$body .= "<strong>System:</strong> ";
		$body .= PHP_OS;
		$body .= "<br/>\n";
		$trace = self::generateTraceMarkup($function, $exception->getTrace());
        if ($trace !== '') $body .= '<h2>Stack Trace:</h2>' . nl2br($trace);
		//$con = get_defined_constants(true);
		//echo nl2br(var_dump($con['user']));
        return self::generateErrorTemplateMarkup('Clomibep Application Exception', $body);
    }

    public static function generateErrorTemplateMarkup($title, $body) {
        $html = "<html><head><title>$title</title><style>body{margin:0;padding:30px;font:12px/1.5 Helvetica,Arial,Verdana,sans-serif;}h1{margin:0;font-size:48px;font-weight:normal;line-height:48px;}strong{display:inline-block;width:65px;}</style></head><body>";
        $html .= "<h1>$title</h1>";
        $html .= $body;
        $html .= '</body></html>';
        return $html;
    }
	
	public static function generateTraceMarkup($function = array(), $trace = NULL){
		$function[] = __FUNCTION__;
		if(!is_array($trace) || $trace == NULL) $trace = debug_backtrace();

		$traceline = '<p style="margin: 0px; padding: 0px;"><font style="color:#2B91AF">#%s</font> <font style="font-weight: bold">%s</font>%s</p><p style="margin: 0px 0px 8px 0px; padding: 0px 0px 0px 20px;"><font style="color: #7A91C9;">%s</font>%s%s(%s);</p>';
		$result = array();

		$continue = true;
		$c = 0;
		foreach ($trace as $key => $stackPoint) {
			if(isset($stackPoint['function']) && in_array($stackPoint['function'], $function) && $continue){
				$c++;
				continue;
			}
			$continue = false;
			$result[] = sprintf(
				$traceline,
				$key - $c,
				(isset($stackPoint['file']) ? $stackPoint['file'] : 'Praser PHP:'),
				(isset($stackPoint['line']) ? '('.$stackPoint['line'].'):' : (isset($stackPoint['file']) ? ':' : '')),
				(isset($stackPoint['class']) ? $stackPoint['class'] : ''),
				(isset($stackPoint['type']) ? $stackPoint['type'] : ''),
				$stackPoint['function'],
				(isset($trace[$key]['args']) ? implode(', ', array_map(array('Markup', 'generateVarMarkup'), $trace[$key]['args'])) : "")
			);
		}
		
		$body = '<hr/>';
		$body .= implode("\n", $result);
		$body .= '<hr/><br/>';
		return $body;
	}
	
	private static function generateVarMarkup($value){
		$type = gettype($value);
		$return = '(<font style="color: #0000FF;">' . $type . '</font>) ';
		if(is_string($value)) $return .= '<font style="color: #C11515;">"' . $value . '"</font>';
		else if(is_bool($value)) $return .= '<font style="color: #0000FF;">'.($value ? "true" : "false").'</font>';
		else if(is_numeric($value)) $return .= '<font style="color: #0000FF;">\''.$value.'\'</font>';
		else if(is_object($value)) $return .= 'of <font style="color: #0000FF;">\''.get_class($value).'\'</font>))';
		else if(is_array($value)) $return .= 'Array';
		else $return .= $value;
		return $return;
	}
}